package com.ccb.sc.order.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccb.sc.auth.TokenModel;
import com.ccb.sc.order.service.ShoppingCartService;
import com.ccb.sc.order.vo.ShoppingCartItemVo;
import com.ccb.sc.common.bind.annotation.CurrentUser;
//psl import com.ccb.sc.common.bind.exception.BizException;
import com.ccb.sc.common.bind.response.ResBody;
import com.ccb.sc.common.code.BizCodeFace;
import com.ccb.sc.common.code.ErrorCode;
import com.ccb.sc.common.modules.shopkeeper.entity.ShoppingCart;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author ken
 * @since 2020-09-24
 */
@RestController
@SaCheckLogin
@RequestMapping("/app/shopping-cart")
@Api(value = "购物车", tags = "购物车")
public class ShoppingCartController {

    private ShoppingCartService shoppingCartService;

    @PostMapping(value = "/list")
    @ApiOperation(value = "购物车列表",response = ShoppingCartItemVo.class)
    public ResBody<?> getCartList(@CurrentUser @ApiIgnore TokenModel token){
        List<ShoppingCartItemVo> shoppingCart = this.shoppingCartService.list(token.getId());
        return ResBody.success(shoppingCart);
    }

    @GetMapping(value = "/quantity")
    @ApiOperation(value = "购物车数量")
    public ResBody<?> count(@CurrentUser @ApiIgnore TokenModel token){
        int count = this.shoppingCartService.count(token.getId());
        return ResBody.success(count);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "加入购物车")
    public ResBody<?> addToCart(@CurrentUser @ApiIgnore TokenModel token,
                                @ApiParam(value = "商品规格id") @RequestParam Long goodsSpecsId,
                                @RequestParam(defaultValue = "1") Integer quantity){
        boolean result = this.shoppingCartService.addToCart(token.getId(), goodsSpecsId, quantity);
        if (!result) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL));
        }
        return ResBody.success();
    }

    @ApiOperation(value = "移除购物车")
    @PostMapping(value = "/remove")
    public ResBody<?> removeProduct(@CurrentUser @ApiIgnore TokenModel token, @RequestParam Long[] goodsSpecsIds){
        Assert.isTrue(goodsSpecsIds != null && goodsSpecsIds.length > 0, "请选择删除商品");
        boolean result = this.shoppingCartService.remove(new QueryWrapper<ShoppingCart>().lambda()
                .in(ShoppingCart::getGoodsSpecsId, Arrays.asList(goodsSpecsIds))
                .eq(ShoppingCart::getMemberId, token.getId()));
        if (!result) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL));
        }
        return ResBody.success();
    }

    @PostMapping(value = "/change-quantity")
    @ApiOperation(value = "改变购物车商品数量")
    public ResBody<?> changeQuantity(@CurrentUser @ApiIgnore TokenModel token, @RequestParam Long goodsSpecsId, @RequestParam Integer quantity){
        boolean result = this.shoppingCartService.changeQuantity(token.getId(), goodsSpecsId, quantity);
        if (!result) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL));
        }
        return ResBody.success();
    }

    @PostMapping(value = "/empty")
    @ApiOperation(value = "清空购物车")
    public ResBody<?> emptyCart(@CurrentUser @ApiIgnore TokenModel token){
        boolean result = this.shoppingCartService.emptyCart(token.getId());
        if (!result) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL));
        }
        return ResBody.success();
    }

    @PostMapping(value = "/select")
    @ApiOperation(value = "改变选中状态")
    public ResBody<?> select(@CurrentUser @ApiIgnore TokenModel token, @RequestParam Long[] goodsSpecsIds, @RequestParam Boolean selected){
        this.shoppingCartService.changeSelect(token.getId(), goodsSpecsIds, selected);
        return ResBody.success();
    }


    @Autowired
    public void setShoppingCartService(ShoppingCartService shoppingCartService) {
        this.shoppingCartService = shoppingCartService;
    }
}

